package cc.rengu.igas.bomp.biz.service.impl;

import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.facade.BompTermManageService;
import cc.rengu.igas.bomp.facade.request.*;
import cc.rengu.igas.bomp.facade.response.*;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class BompTermManageServiceImpl implements BompTermManageService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<MPOSInstListQueryResponse> mPOSInstListQuery(MPOSInstListQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MPOSInstListQueryResponse> result = new Result<>();
        MPOSInstListQueryResponse mPOSInstListQueryResponse = new MPOSInstListQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MPOSInstListQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MPOSInstListQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("查询母POS机构列表处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mPOSInstListQueryResponse.setRespCode(((BizException) e).getCode());
                mPOSInstListQueryResponse.setRespDesc(e.getMessage());
            } else {
                mPOSInstListQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mPOSInstListQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<RegisterTermResponse> registerTerm(RegisterTermRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<RegisterTermResponse> result = new Result<>();
        RegisterTermResponse registerTermResponse = new RegisterTermResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, RegisterTermResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((RegisterTermResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("注册终端设备处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                registerTermResponse.setRespCode(((BizException) e).getCode());
                registerTermResponse.setRespDesc(e.getMessage());
            } else {
                registerTermResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                registerTermResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<GeneratorTermValidCodeResponse> generatorTermValidCode(GeneratorTermValidCodeRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<GeneratorTermValidCodeResponse> result = new Result<>();
        GeneratorTermValidCodeResponse generatorTermValidCodeResponse = new GeneratorTermValidCodeResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, GeneratorTermValidCodeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((GeneratorTermValidCodeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("生成终端装机验证码处理失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                generatorTermValidCodeResponse.setRespCode(((BizException) e).getCode());
                generatorTermValidCodeResponse.setRespDesc(e.getMessage());
            } else {
                generatorTermValidCodeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                generatorTermValidCodeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<MchntTermInfoQueryResponse> mchntTermInfoQuery(MchntTermInfoQueryRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntTermInfoQueryResponse> result = new Result<>();
        MchntTermInfoQueryResponse mchntTermInfoQueryResponse = new MchntTermInfoQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MchntTermInfoQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntTermInfoQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户终端信息查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntTermInfoQueryResponse.setRespCode(((BizException) e).getCode());
                mchntTermInfoQueryResponse.setRespDesc(e.getMessage());
            } else {
                mchntTermInfoQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntTermInfoQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<MchntTermInfoUpdateResponse> mchntTermInfoUpdate(MchntTermInfoUpdateRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntTermInfoUpdateResponse> result = new Result<>();
        MchntTermInfoUpdateResponse mchntTermInfoUpdateResponse = new MchntTermInfoUpdateResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, MchntTermInfoUpdateResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((MchntTermInfoUpdateResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户终端信息查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                mchntTermInfoUpdateResponse.setRespCode(((BizException) e).getCode());
                mchntTermInfoUpdateResponse.setRespDesc(e.getMessage());
            } else {
                mchntTermInfoUpdateResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                mchntTermInfoUpdateResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<TermInfoReportOrUpdateResponse> termInfoReportOrUpdate(TermInfoReportOrUpdateRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<TermInfoReportOrUpdateResponse> result = new Result<>();
        TermInfoReportOrUpdateResponse termInfoReportOrUpdateResponse = new TermInfoReportOrUpdateResponse();
        try {
            DubboResult dubboResult = dubboService.callBizService(request, TermInfoReportOrUpdateResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((TermInfoReportOrUpdateResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("终端信息报备及更新失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                termInfoReportOrUpdateResponse.setRespCode(((BizException) e).getCode());
                termInfoReportOrUpdateResponse.setRespDesc(e.getMessage());
            } else {
                termInfoReportOrUpdateResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                termInfoReportOrUpdateResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
